import axios from "axios";
import { Notify } from "vant";
import { getToken } from "./auth";

// 创建实例 可以使用自定义配置新建一个 axios 实例
const instance = axios.create({
    baseURL: "http://lanlianhua.work:3009", //axios封装全局拦截,
    timeout: 5000,
  });
  instance.interceptors.request.use(
    function (config) {
    if(getToken()){
      config.headers.authorization = `Bearer ${getToken()}`;
    }
    return config;
  }, 
  function(error){
    return Promise.reject(error);
  }
  )
  // 响应拦截器
  instance.interceptors.response.use(
    function (response) {
    // 在发送请求之前做些什么
    return response.data;
  }, 
  function (error) {
    console.log("error",error.response);
    const { status }=error.response;
    if(status===401){
        Notify({
            type:"warning",
            message:"未登录，请先登录"
        });
        window.location.href="#/login";
    }
    // 对请求错误做些什么
    return Promise.reject(error);
  }
  );
// 封装get请求
export const get=(url,params)=>instance.get(url,{params:params});
// 封装post请求
export const post=(url,data)=>instance.post(url,data);
// 删除
export const del=(url,params)=>instance.delete(url,{
  params});
export default instance;